<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	request.setAttribute("basePath", path);
%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Faurecia</title>
    <link rel="stylesheet" type="text/css" href="${basePath}/resource/jquery-easyui/themes/default/easyui1.css">
    <link rel="stylesheet" type="text/css" href="${basePath}/resource/jquery-easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="${basePath}/resource/jquery-easyui/themes/color.css">
    <link rel="stylesheet" type="text/css" href="${basePath}/resource/jquery-easyui/demo/demo.css">
    <script type="text/javascript" src="${basePath}/resource/jquery-easyui/jquery.min.js"></script>
    <script type="text/javascript" src="${basePath}/resource/jquery-easyui/jquery.easyui.min.js"></script>
    <style type="text/css">
    	#ff table tr td{
    		height:50px;
    	}
    	.hidenDiv {
    		width:100%
    	}
    	.hidenDiv tr{
    		height:80px;
    		margin-top:40px;
    	}
    	.hidenDiv tr td{
    		width:100%
    	}
    	.txt{
    		font-size:36px;	
    		font-weight:300
    	}
    	.l-btn-text{
    		font-size:26px;	
    	}
    </style>
    <script type="text/javascript">
    	var gcode = "";
    	var gindex = 0;
	    $(document).ready(function() {
	    	scanHidenFun();
	    	$('#scanCode').textbox('textbox').keydown(function (e) {
                if (e.keyCode == 13) {
                	showErrorMsg("");
                	checkErrorMsgFnc(-1,"");
                	var code = $("#scanCode").val();
                	if(gindex > 3){
                		showErrorMsg("扫码已完成！");
                		return false;
                	}
					if(code){
						$.ajax({
							url:"${basePath}/instock/scanCode/",
							data:{
								code:code
							},
							async:false,
							dataType : "json",
							success : function(data) {
								console.info(data);
								if(data.result == 0){
									/* clearFun(); */
									$('#scanCode').textbox('clear');
									scanShowFun(code);
								}else{
									showErrorFnc();
									//$.messager.alert('错误','<p style="color:red">条码错误，请重新扫描<p>');    
									showErrorMsg(data.msg);
								}
							},
							error : function() {
							}
						})
					}
                }
            });
	    	
		});
	    
	    function scanShowFun(code){
	    	if(!gcode){
	    		gcode = code.substring(0,10) ;
	    	}else{
	    		if(gcode != code.substring(0,10) ){
	    			showErrorFnc();
	    			//$.messager.alert('错误','<p style="color:red">条码错误，请重新扫描<p>');
	    			showErrorMsg("条码错误，请重新扫描");
	    			return false;
	    		}
	    	}
	    	if(gindex == 0){
	    		$("#index0").css("background-color","green");
	    		$("#code0").val(code);
	    	}
	    	if(gindex == 1){
	    		if(code == $("#code0").val()){
	    			showErrorFnc();
	    			showErrorMsg("条码重复，请重新扫描");
	    			return false;
	    		}
	    		$("#index1").css("background-color","green");
	    		$("#code1").val(code);
	    	}
	    	if(gindex == 2){
	    		if(code == $("#code0").val() || code == $("#code1").val()){
	    			showErrorFnc();
	    			showErrorMsg("条码重复，请重新扫描");
	    			return false;
	    		}
	    		$("#index2").css("background-color","green");
	    		$("#code2").val(code);
	    	}
	    	if(gindex == 3){
	    		if(code == $("#code0").val() || code == $("#code1").val() || code == $("#code2").val()){
	    			showErrorFnc();
	    			showErrorMsg("条码重复，请重新扫描");
	    			return false;
	    		}
	    		$("#index3").css("background-color","green");
	    		$("#code3").val(code);
	    	}
	    	gindex++;
	    }
	    
	    function showErrorFnc(){
	    	if(gindex == 0){
	    		$("#index0").css("background-color","red");
	    		$("#code0").val("");
	    	}
	    	if(gindex == 1){
	    		$("#index1").css("background-color","red");
	    		$("#code1").val("");
	    	}
	    	if(gindex == 2){
	    		$("#index2").css("background-color","red");
	    		$("#code2").val("");
	    	}
	    	if(gindex == 3){
	    		$("#index3").css("background-color","red");
	    		$("#code3").val("");
	    	}
	    }
	    
	    function scanHidenFun(){
	    	gcode = "";
	    	gindex = 0;
    		$("#index0").css("background-color","#ccc");
    		$("#index1").css("background-color","#ccc");
    		$("#index2").css("background-color","#ccc");
    		$("#index3").css("background-color","#ccc");
    		$("#code0").val("");
    		$("#code1").val("");
    		$("#code2").val("");
    		$("#code3").val("");
    		$("#error").html("");
	    }
	    function showErrorMsg(msg){
	    	$("#error").html(msg);
	    }
	</script>
</head>
<body>
	<div >
	    <form id="ff" method="post">
	    	<table cellpadding="5">
	    		<tr>
	    			<td  class="txt" align="right" style="width:200px;">目标位置:</td>
	    			<td>
	    				<select class="easyui-combobox" id="stock" name="stock" data-options="panelHeight:'auto',editable:false" style="width:350px;height:50px;">
	    					<option value="SHOPSTOCK">SHOPSTOCK</option>
	    					<option value="POOLSTOCK">POOLSTOCK</option>
    					</select>
	    			</td>
	    		</tr>
	    		<tr>
	    			<td class="txt" align="right">扫描:</td>
	    			<td><input class="easyui-textbox" type="text" id="scanCode" name="items" style="width:350px;height:50px"></input></td>
	    			<td>
	    				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearFun()" style="width:100px;height:40px">清空</a>
	    			</td>
	    		</tr>
	    		<tr>
	    			<td colspan="2" style="width:300px;">
	    				<table cellpadding="5" class = "hidenDiv" style="border-collapse:separate; border-spacing:10px;">
			    			<tr>
				    			<td colspan="2" style="background-color:#ccc" align="center" id="index0">
				    				<input type="hidden" id="code0"/>
				    			</td>
				    		</tr>
				    		<tr>
				    			<td colspan="2" style="background-color:#ccc" align="center" id="index1">
				    				<input type="hidden" id="code1"/>
				    			</td>
				    		</tr>
				    		<tr>
				    			<td colspan="2" style="background-color:#ccc" align="center" id="index2">
				    				<input type="hidden" id="code2"/>
				    			</td>
				    		</tr>
				    		<tr>
				    			<td colspan="2" style="background-color:#ccc" align="center" id="index3">
				    				<input type="hidden" id="code3"/>
				    			</td>
				    		</tr>
				    		<tr>
				    			<td colspan="2" align="center" id="error" style="font-size:26px;color:red">
				    				
				    			</td>
				    		</tr>
			    		</table>
	    			</td>
	    			<td colspan="2" style="width:600px;">
	    				<table cellpadding="5" style="border-collapse:separate; border-spacing:0px;">
			    			<tr>
				    			<td style="font-size:30px" align="right">项目：</td>
				    			<td>
				    				<select class="easyui-combobox" id="prog" name="prog" data-options="panelHeight:'auto',editable:false" style="width:350px;height:50px;">
				    					<option value="TX9">TX9</option>
				    					<option value="W23R">W23R</option>
				    					<option value="A94">A94</option>
				    					<option value="A94R">A94R</option>
				    					<option value="T93">T93</option>
				    					<option value="T91">T91</option>
			    					</select>
				    			</td>
				    		</tr>
				    		<tr>
				    			<td style="font-size:30px" align="right">产品：</td>
				    			<td>
				    				<input class="easyui-combobox" id="itemCode" name="itemCode" data-options="panelHeight:'300px',editable:false,valueField:'itemCode',textField:'itemCode',url:'queryAll'" style="width:350px;height:50px;"/> 
				    			</td>
				    		</tr>
				    		<tr>
				    			<td style="font-size:30px" align="right">配置：</td>
				    			<td>
				    				<input class="easyui-combobox" id="itemName" name="itemName" data-options="panelHeight:'300px',editable:false,valueField:'itemName',textField:'itemName',url:'queryAll'" style="width:350px;height:50px;"/>
			    				</td>
				    		</tr>
				    		<tr>
				    			<td style="font-size:30px" align="right">颜色：</td>
				    			<td>
				    				<!-- <select class="easyui-combobox" id="itemNameShort" name="itemNameShort"  data-options="panelHeight:'auto',editable:false" style="width:350px;height:50px;">
				    					<option value="#FFFF66">黄色</option>
				    					<option value="#FFC000">深橘黄</option>
				    					<option value="#B1A0C7">紫色</option>
				    					<option value="#0066FF">蓝色</option>
				    					<option value="#92CDDC">浅蓝色</option>
			    					</select> -->
			    					<input class="easyui-combobox" id="itemNameShort" name="itemNameShort" data-options="panelHeight:'300px',editable:false,valueField:'itemNameShort',textField:'itemNameShort',url:'queryAll'" style="width:350px;height:50px;"/>
			    				</td>
				    		</tr>
				    		<tr>
				    			<td></td>
				    			<td>
				    				<a id="btn" href="javascript:void(0);" class="easyui-linkbutton" style="width:150px;height:40px" onclick="checkFnc()" >确认描述</a> 
				    			</td>
				    		</tr>
				    		<tr>
				    			<td colspan="2" id="checkError" align="center" style="font-size:26px;">
				    				
				    			</td>
				    		</tr>
			    		</table>
	    			</td>
	    		</tr>
    		</table>
	    </form>
    </div>
    <script>
	    function clearFun(){
		   	$('#scanCode').textbox('clear');
		   	scanHidenFun();
		   	checkErrorMsgFnc(-1,"");
	    }
		function checkFnc(){
			var code = gcode;
			var prog = $("#prog").combobox('getValue');
			var itemCode = $("#itemCode").combobox('getValue');
			var itemName = $("#itemName").combobox('getValue');
			var itemNameShort = $("#itemNameShort").combobox('getValue');
			if(!prog || !itemCode || !itemName || !itemNameShort){
				checkErrorMsgFnc(1,"产品信息不全");
				return false;
			}
			if(gindex < 4){
				/* showErrorMsg("条码数量不足"); */
				checkErrorMsgFnc(1,"条码数量不足");
				return false;
			}
			/* var whItem = {
				code:code,
				prog:prog,
				itemCode:itemCode,
				itemName:itemName,
				itemNameShort:itemNameShort	
			} */
			$.ajax({
				url:"${basePath}/instock/getItem/",
				data:{
					code:code
				},
				async:false,
				dataType : "json",
				success : function(data) {
					if(!data){
						/* showErrorMsg('错误','<p style="color:red">验证错误，请重新输入产品信息<p>');   */
						checkErrorMsgFnc(1,"验证错误，请重新输入产品信息");
					}else{
						if(data.prog != prog || data.itemCode != itemCode || data.itemName != itemName || data.itemNameShort != itemNameShort){
							checkErrorMsgFnc(1,"验证错误，请重新输入产品信息");
						}else{
							checkErrorMsgFnc(0,"");  
							$.messager.confirm('确认','验证一致，请入库',function(r){    
							    if (r){    
							    	inStockFnc(code);
							    }    
							});
						}
					}
				},
				error : function() {
					checkErrorMsgFnc(1,"验证错误，请重新输入产品信息");
				}
			})
		}
		
		function inStockFnc(code){
			var code0 = $("#code0").val();
			var code1 = $("#code1").val();
			var code2 = $("#code2").val();
			var code3 = $("#code3").val();
			var stock = $("#stock").combobox('getValue');
			
			$.ajax({
				url:"${basePath}/instock/instock/",
				data:{
					code0:code0,	
					code1:code1,	
					code2:code2,	
					code3:code3,
					stock:stock
				},
				async:false,
				dataType : "json",
				success : function(data) {
					if(data.result == 0){
						clearFun();
						checkErrorMsgFnc(0,"入库成功");  
						/* scanHidenFun(); */
					}else{
						checkErrorMsgFnc(1,"入库失败");  
					}
				},
				error : function() {
					checkErrorMsgFnc(1,"入库失败");  
				}
			})
		}
		function checkErrorMsgFnc(flag,msg){
			if(flag == 0){
				$("#checkError").css("background-color","green");
				$("#checkError").html(msg);
			}else if(flag == 1){
				$("#checkError").css("background-color","red");
				$("#checkError").html(msg);
			}else{
				$("#checkError").css("background-color","#fff");
				$("#checkError").html("");
			}
				
		}
	</script>
</body>
</html>